package backTrack.review5_1;

import com.sun.deploy.uitoolkit.impl.fx.AppletStageManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Class90 {


    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<Integer>();
    boolean[] flag;

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        flag = new boolean[nums.length];
        Arrays.fill(flag, false);
        Arrays.sort(nums);
        backTracking(nums,0);
        return result;
    }

    public void backTracking(int[] nums, int start) {
        //  CKG TODO 2023/4/30:  上来加入path可以把空集加入进来
        result.add(new ArrayList<>(path));
        if (start>= nums.length){
            return;
        }
        for (int i = start; i < nums.length ; i++) {
            //  CKG TODO 2023/4/30:  加入的顺序是 1  12  123  这样子的顺序
            if (i>0&&nums[i]==nums[i-1]&&!flag[i-1]){
                continue;
            }
            flag[i]=true;
            path.add(nums[i] );
            backTracking(nums,i+1);
            path.removeLast();
            flag[i]=false;
        }

    }

    public static void main(String[] args) {
        Class90 class90 = new Class90();
        int[] arr={1,2,2};
        System.out.println(class90.subsetsWithDup(arr).toString());


    }

}
